//
//  HTGroupViewController.m
//  HeartTravel
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 tantan. All rights reserved.
//

#import "HTGroupViewController.h"
#import "HTDrawerView.h"
#import "HTMainCollectionViewCell.h"
#import "PKRequestManager.h"
#import "HTGroupMainListModel.h"
#import "HTCityIntroViewController.h"
#define kHotURL @"http://api.breadtrip.com/destination/index_places/2/"
#define kEuropeURL @"http://api.breadtrip.com/destination/index_places/3/"
#define kGatURL @"http://api.breadtrip.com/destination/index_places/4/"
#define kamericasURL @"http://api.breadtrip.com/destination/index_places/5/"
#define kAsiaURL @"http://api.breadtrip.com/destination/index_places/6/"
#define kChinaURL @"http://api.breadtrip.com/destination/index_places/8/"
@interface HTGroupViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) HTDrawerView *dView;
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@end

@implementation HTGroupViewController
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)requestDataSource:(NSString *)urlString{
    [PKRequestManager requestWithType:GET urlString:urlString parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];

        for (NSDictionary *dic in dict[@"data"]) {
            HTGroupMainListModel *model = [[HTGroupMainListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainCollectionView reloadData];
    } error:^(NSError *error) {
        NSLog(@"导游主界面数据请求失败%@", error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestDataSource:kChinaURL];
    
    self.navigationController.navigationBar.hidden = YES;
    self.titleLabel.text = @"国内";
    [self.rightButton setBackgroundImage:[UIImage imageNamed:@"抽屉"] forState:(UIControlStateNormal)];
    self.rightButton.frame = CGRectMake(SCREEN_WIDTH - 35, 13, 25, 25);
    [self.rightButton addTarget:self action:@selector(didClickShowDrawer:) forControlEvents:(UIControlEventTouchUpInside)];
    self.dView = [[[UINib nibWithNibName:@"HTDrawView" bundle:nil]instantiateWithOwner:self options:nil] firstObject];
    _dView.frame = CGRectMake(SCREEN_WIDTH - 80, 70, 80, 240);
    _dView.alpha = 0.7;
    // 给这个控制器添加轻拍手势
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapToRemoveDview:)];
//    [self.view addGestureRecognizer:tap];
    // 给Button添加点击事件
    [self.dView.hotButton addTarget:self action:@selector(didClickToHot) forControlEvents:(UIControlEventTouchUpInside)];
    [self.dView.chinaButton addTarget:self action:@selector(didClickToChina) forControlEvents:(UIControlEventTouchUpInside)];
    [self.dView.hatButton addTarget:self action:@selector(didClickToGat) forControlEvents:(UIControlEventTouchUpInside)];
    [self.dView.asiaButton addTarget:self action:@selector(didClickToAsia) forControlEvents:(UIControlEventTouchUpInside)];
    [self.dView.europeButton addTarget:self action:@selector(didClickToEurope) forControlEvents:(UIControlEventTouchUpInside)];
    [self.dView.americasButton addTarget:self action:@selector(didClickToAmericas) forControlEvents:(UIControlEventTouchUpInside)];
    [self addCollectionView];
}
// 点击抽屉Button重新请求数据
- (void)didClickToHot{
    [self.dataSourceArray removeAllObjects];
    [self requestDataSource:kHotURL];
    self.titleLabel.text = @"热门";
}
- (void)didClickToChina{
    [self.dataSourceArray removeAllObjects];
    [self requestDataSource:kChinaURL];
    self.titleLabel.text = @"国内";
}
- (void)didClickToGat{
    [self.dataSourceArray removeAllObjects];
    [self requestDataSource:kGatURL];
    self.titleLabel.text = @"港澳台";
}
- (void)didClickToAsia{
    [self.dataSourceArray removeAllObjects];
    [self requestDataSource:kAsiaURL];
    self.titleLabel.text = @"亚洲";
}
- (void)didClickToEurope{
    [self.dataSourceArray removeAllObjects];
    [self requestDataSource:kEuropeURL];
    self.titleLabel.text = @"欧洲";
}
- (void)didClickToAmericas{
    [self.dataSourceArray removeAllObjects];
    [self requestDataSource:kamericasURL];
    self.titleLabel.text = @"美洲大洋洲";
}
//- (void)didTapToRemoveDview:(UITapGestureRecognizer *)tap{
//    [self.dView removeFromSuperview];
//    self.rightButton.selected = NO;
//}

- (void)didClickShowDrawer:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self.view addSubview:self.dView];
    }else{
        [self.dView removeFromSuperview];
    }
}

- (void)addCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 60)/2, (SCREEN_WIDTH - 60)/2);
    layout.minimumInteritemSpacing = 20;
    layout.minimumLineSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    _mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT - 70) collectionViewLayout:layout];
    _mainCollectionView.backgroundColor = [UIColor clearColor];
    _mainCollectionView.dataSource = self;
    _mainCollectionView.delegate = self;
    [self.mainCollectionView registerClass:[HTMainCollectionViewCell class] forCellWithReuseIdentifier:@"HTMainCollectionViewCell"];
    [self.view addSubview:_mainCollectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HTMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HTMainCollectionViewCell" forIndexPath:indexPath];
    cell.configureModel = self.dataSourceArray[indexPath.row];
    cell.imageView.userInteractionEnabled = YES;
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREEN_WIDTH - 60)/2, (SCREEN_WIDTH - 60)/2);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.dView removeFromSuperview];
    self.rightButton.selected = NO;
    HTGroupMainListModel *model = self.dataSourceArray[indexPath.row];
    HTCityIntroViewController *cityIntroVC = [[HTCityIntroViewController alloc]init];
    cityIntroVC.titleName = model.name;
    cityIntroVC.posterUrl = model.cover;
    NSString *urlString = [model.url substringFromIndex:7];
//    NSLog(@"===%@", urlString);
    cityIntroVC.urlString = urlString;
    [self.navigationController pushViewController:cityIntroVC animated:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
